/*********************************************************************
* File: debug.h
*
* \brief Useful functions for debugging.
*
* \author Instituto Superior Tecnico de Lisboa, Portugal
* \author Technical University of Lisbon, Portugal
* 
* \author Agentes Autonomos e Sistemas Multiagente
* \author Autonomous Agents and Multiagent Systems
* 
* \version	1.0
* \date		2006/2007
*********************************************************************/
#include "debug.h"
#include "util.h"
using namespace util;

#include <time.h>

FILE* g_debugLog = 0;

/**
* \brief Should be invoked when the application starts.
**/
void util::debug::initialize(){
#ifdef DEBUGLOG
	char szFileName[256];
	util::getModDirectory(szFileName);
	strcat(szFileName, "logs/");

	char tmpbuf[128];
    __time64_t ltime;
    struct tm *today;

    _time64( &ltime );
    today = _localtime64( &ltime );
    strftime( tmpbuf, 128, "%d_%b_%y_%H_%M_%S", today );
	strcat(szFileName, tmpbuf);
	strcat(szFileName, ".txt");

	g_debugLog = fopen(szFileName, "wb");
#endif
}

/**
* \brief Should be invoked when the application ends.
**/
void util::debug::conclude(){
#ifdef DEBUGLOG
	if(g_debugLog != NULL)
		fclose(g_debugLog);
	g_debugLog = NULL;
#endif
}

/**
* \brief Logs a message to the log file.
*
* \param[in] msg The message to log.
**/
void util::debug::log(const char* msg){
#ifdef DEBUGLOG
	char timebuf[128];
	_strtime( timebuf );

	char logMsg[512];
	strcpy(logMsg, timebuf);
	strcat(logMsg, ": ");
	strcat(logMsg, msg);
	strcat(logMsg, "\r\n");
	fwrite(logMsg, strlen(logMsg), 1, g_debugLog);
	fflush(g_debugLog);
#endif
}
/**
* \brief Logs a message to the log file.
*
* \param[in] prefix Prefix to append to the message.
* \param[in] msg The message to log.
**/
void util::debug::log(const char* prefix, const char* msg){
#ifdef DEBUGLOG
	char logMsg[512];
	strcpy(logMsg, prefix);
	strcat(logMsg, ":");
	strcat(logMsg, msg);
	util::debug::log(logMsg);
#endif
}
/**
* \brief Logs a message to the log file.
*
* \param[in] msg The message to log.
**/
void util::debug::logWrite(const char* msg){
#ifdef DEBUGLOG
	fwrite(msg, strlen(msg), 1, g_debugLog);
	fflush(g_debugLog);
#endif
}
/**
* \brief Logs a message and a 'newline' to the log file.
*
* \param[in] msg The message to log.
**/
void util::debug::logWriteLn(const char* msg){
#ifdef DEBUGLOG
	char logMsg[512];
	strcpy(logMsg, msg);
	strcat(logMsg, "\r\n");
	fwrite(logMsg, strlen(logMsg), 1, g_debugLog);
	fflush(g_debugLog);
#endif
}
/**
* \brief Logs a 'newline' to the log file.
**/
void util::debug::logWriteNewline(){
#ifdef DEBUGLOG
	fwrite("\r\n", strlen("\r\n"), 1, g_debugLog);
	fflush(g_debugLog);
#endif
}
/**
* \brief Logs a number to the log file.
*
* \param[in] number The number.
**/
void util::debug::logWrite(int number){
#ifdef DEBUGLOG
	char logMsg[64];
	itoa(number, logMsg, 10);
	fwrite(logMsg, strlen(logMsg), 1, g_debugLog);
	fflush(g_debugLog);
#endif
}

/**
* \brief Prints a message on the server side. 
*
* \param[in] msg The message to print.
**/
void util::debug::printToServer(const char* msg){
	SERVER_PRINT(msg);
	util::debug::log(msg);
}

/**
* \brief Prints a message on a Windows message box. 
*
* \param[in] title The message box title.
* \param[in] msg The message box string content.
**/
void util::debug::printToMessageBox(const char* title, const char* msg)
{
#ifdef _WIN32
   MessageBox(0, msg, title, MB_ICONERROR);
#endif
   util::debug::log(msg);
}

/**
* \brief Print a message on the client side.
*
* \param[in] pEdict The client.
* \param[in] ptype One of: (1) 'print_console'; (2) 'print_center';	(3) 'print_chat'.
* \param[in] msg The message.
**/
void util::debug::printToClient(edict_t* pEdict, PRINT_TYPE ptype, const char *msg)
{
	util::hostPrint(pEdict, ptype, msg);
	util::debug::log(msg);
}

/**
* \brief Print an alert message.
*
* \param[in] aType One of:	(1)at_notice; (2)at_console; (3)at_aiconsole; 
						(4)at_warning; (5)at_error; (6)at_logged.
* \param[in] msg The message.
**/
void util::debug::alertMessage(ALERT_TYPE atype, char *msg){
	ALERT(atype, msg);
   util::debug::log(msg);
}